package org.csu.canteen.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.csu.canteen.vo.AccountVO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Data
@TableName("orders")
public class Orders {
    @TableId(value = "orderid")
    private Integer orderId;

    @TableField(value = "userid")
    private String username;

    @TableField(value = "orderdate")
    private Date orderDate;

    @TableField(value = "shipaddr1")
    private String shipAddress1;

    @TableField(value = "shipaddr2")
    private String shipAddress2;

    @TableField(value = "shipcity")
    private String shipCity;

    @TableField(value = "shipstate")
    private String shipState;

    @TableField(value = "shipzip")
    private String shipZip;

    @TableField(value = "shipcountry")
    private String shipCountry;

    @TableField(value = "billaddr1")
    private String billAddress1;

    @TableField(value = "billaddr2")
    private String billAddress2;

    @TableField(value = "billcity")
    private String billCity;

    @TableField(value = "billstate")
    private String billState;

    @TableField(value = "billzip")
    private String billZip;

    @TableField(value = "billcountry")
    private String billCountry;

    private String courier;

    @TableField(value = "totalprice")
    private BigDecimal totalPrice;

    @TableField(value = "billtofirstname")
    private String billToFirstName;

    @TableField(value = "billtolastname")
    private String billToLastName;

    @TableField(value = "shiptofirstname")
    private String shipToFirstName;

    @TableField(value = "shiptolastname")
    private String shipToLastName;

    @TableField(value = "creditcard")
    private String creditCard;

    @TableField(value = "exprdate")
    private String expiryDate;

    @TableField(value = "cardtype")
    private String cardType;

    private String locale;

    @TableField(exist = false) // 非数据库字段
    private List<LineItem> lineItems = new ArrayList<>();

    @TableField(exist = false) // 非数据库字段
    private String status;

    // 初始化订单
    public void initOrder(AccountVO account, List<CartItem> cart) {
        username = account.getUsername();
        orderDate = new Date();

        shipToFirstName = account.getFirstName();
        shipToLastName = account.getLastName();
        shipAddress1 = account.getAddress1();
        shipAddress2 = account.getAddress2();
        shipCity = account.getCity();
        shipState = account.getState();
        shipZip = account.getZip();
        shipCountry = account.getCountry();

        billToFirstName = account.getFirstName();
        billToLastName = account.getLastName();
        billAddress1 = account.getAddress1();
        billAddress2 = account.getAddress2();
        billCity = account.getCity();
        billState = account.getState();
        billZip = account.getZip();
        billCountry = account.getCountry();

        // **计算 totalPrice**
        totalPrice = cart.stream()
                .map(CartItem::getTotal) // 获取每个 CartItem 的 total
                .filter(Objects::nonNull) // 过滤掉 null 避免报错
                .reduce(BigDecimal.ZERO, BigDecimal::add); // 累加所有 total

        creditCard = "999 9999 9999 9999";
        expiryDate = "12/03";
        cardType = "Visa";
        courier = "UPS";
        locale = "CA";
        status = "P";

        for (CartItem cartItem : cart) {
            addLineItem(cartItem);
        }
    }

    // 添加订单明细
    public void addLineItem(CartItem cartItem) {
        LineItem lineItem = new LineItem(lineItems.size() + 1, cartItem);
        lineItems.add(lineItem);
    }



    public void addLineItem(LineItem lineItem) {
        lineItems.add(lineItem);
    }
}
